package com.oner365.test.socket.keepalive;

import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LongConnectionExecute extends Thread {

  private static final Logger LOGGER = LoggerFactory.getLogger(LongConnectionExecute.class);

  private Socket threadClient;
  
  

  public LongConnectionExecute(Socket threadClient) {
    this.setThreadClient(threadClient);
  }

  public void run() {
    try {
      Date day = new Date();
      SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      LOGGER.info(df.format(day));
      int index = 1;
      byte[] data = new byte[1024];
      while (true) {
        int len = getThreadClient().getInputStream().read(data);
        if (len > 0) {
          String content = new String(data, 0, len);
          LOGGER.info("客户端IP:{},端口:{},body:{}", getThreadClient().getInetAddress(), getThreadClient().getPort(),
              content);
          String responseBody = "返回"+index+"参数";
          getThreadClient().getOutputStream().write(responseBody.getBytes());
          index++;
          if ("shutdown".equals(content)) {
            break;
          }
        }
      }
    } catch (Exception e) {
      LOGGER.error("connection execute error:", e);
    }
  }

  public Socket getThreadClient() {
    return threadClient;
  }

  public void setThreadClient(Socket threadClient) {
    this.threadClient = threadClient;
  }

}
